<template>
    <div style="margin-top: 20px; border: 1px solid #eee">
        <div style="margin-top: 20px; text-align: left">
            <span style="margin:20px">测试案例</span>
            <el-button @click="toggleSelection(tableData)">全选</el-button>
            <el-button @click="toggleSelection()">取消选择</el-button>
            <el-button @click="run()">运行</el-button>
        </div>
        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="margin-top: 20px; width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    align="center"
                    width="50px">
            </el-table-column>
            <el-table-column
                    prop="id"
                    align="center"
                    label="序号"
                    width="50px">
            </el-table-column>
            <el-table-column
                    prop="caseNum"
                    align="center"
                    label="编号"
                    min-width="12%">
            </el-table-column>
            <el-table-column
                    prop="nameEN"
                    align="center"
                    label="英文名"
                    min-width="16%">
            </el-table-column>
            <el-table-column
                    prop="nameCN"
                    align="center"
                    label="中文名"
                    min-width="12%">
            </el-table-column>
            <el-table-column
                    prop="state"
                    align="center"
                    label="测试状态"
                    min-width="10%">
            </el-table-column>
            <el-table-column
                    prop="result"
                    align="center"
                    label="测试结果"
                    min-width="10%">
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        name: "TestCase",
        data() {
            return {
                multipleSelection: [],
                multiSelecNum: '',
                testFail: [],
                testPass: [],
                idNumMap : {"3.1.130":1, "3.1.060":2, "3.1.270":3, "3.1.280":4, "3.1.290":5,
                            "3.1.310":6, "3.1.170":7, "3.1.180":8, "3.1.160":9, "2.1.020":10,
                            "2.1.100":11, "2.1.090":12, "1.1.180":13, "2.1.120":14, "2.1.010":15,
                            "3.1.050":16, "2.1.130":17, "3.1.200":18, "3.1.190":19, "3.1.100":20,
                            "3.1.040":21, "3.1.120":22, "3.1.110":23, "3.1.140":24, "3.1.090":25,
                            "3.1.080":26, "3.1.070":27, "3.1.030":28, "3.1.020":29, "3.1.010":30},
                tableData: [
                    {
                        "id": 1,
                        "caseNum": "3.1.130",
                        "nameEN": "System Variable Manipulation",
                        "nameCN": "系统变量修改",
                        "state": "READY",
                        "result": "UNKNOWN"
                    }, {
                        "id": 2,
                        "caseNum": "3.1.060",
                        "nameEN": "Switch Identification Spoofing",
                        "nameCN": "交换机欺骗识别",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 3,
                        "caseNum": "3.1.270",
                        "nameEN": "Link-Discovery-Neutralization",
                        "nameCN": "链路发现中止",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 4,
                        "caseNum": "3.1.280",
                        "nameEN": "Flow-Rule-Obstruction",
                        "nameCN": "流规则阻塞",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 5,
                        "caseNum": "3.1.290",
                        "nameEN": "Host-Tracking-Neutralization",
                        "nameCN": "主机位置追踪中止",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 6,
                        "caseNum": "3.1.310",
                        "nameEN": "Service-Unregistration",
                        "nameCN": "服务取消注册",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 7,
                        "caseNum": "3.1.170",
                        "nameEN": "Eavesdrop",
                        "nameCN": "消息窃取",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 8,
                        "caseNum": "3.1.180",
                        "nameEN": "Man-In-The-Middle",
                        "nameCN": "中间人攻击",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 9,
                        "caseNum": "3.1.160",
                        "nameEN": "Link Fabrication attack",
                        "nameCN": "链路伪造",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 10,
                        "caseNum": "2.1.020",
                        "nameEN": "Malformed Control Message",
                        "nameCN": "畸形控制消息",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 11,
                        "caseNum": "2.1.100",
                        "nameEN": "Control Message Manipulation",
                        "nameCN": "错误版本报文",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 12,
                        "caseNum": "2.1.090",
                        "nameEN": "Sequence and Data-Forge",
                        "nameCN": "乱序和数据伪造报文",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 13,
                        "caseNum": "1.1.180",
                        "nameEN": "Stats-Payload-Manipulation",
                        "nameCN": "畸形STATS-REQUEST报文",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 14,
                        "caseNum": "2.1.120",
                        "nameEN": "Echo-Reply-Payload-Manipulation",
                        "nameCN": "畸形ECHO_REPLY报文",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 15,
                        "caseNum": "2.1.010",
                        "nameEN": "malformed version number attack",
                        "nameCN": "畸形头部报文",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 16,
                        "caseNum": "3.1.050",
                        "nameEN": "Switch Table Flooding",
                        "nameCN": "握手报文洪泛",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 17,
                        "caseNum": "3.1.010",
                        "nameEN": "PACKET IN Flooding",
                        "nameCN": "PACKET IN洪泛",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 18,
                        "caseNum": "3.1.020",
                        "nameEN": "Control Message Drop",
                        "nameCN": "控制消息丢弃",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 19,
                        "caseNum": "3.1.030",
                        "nameEN": "Control Message Infinite Loop",
                        "nameCN": "应用陷入死循环",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 20,
                        "caseNum": "3.1.070",
                        "nameEN": "Flow Rule Modification",
                        "nameCN": "流规则修改",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 21,
                        "caseNum": "3.1.080",
                        "nameEN": "Flow Table Clearance",
                        "nameCN": "流规则清除",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 22,
                        "caseNum": "3.1.090",
                        "nameEN": "Event Listener Unsubscription",
                        "nameCN": "取消事件监听订阅",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 23,
                        "caseNum": "3.1.140",
                        "nameEN": "System Command Execution",
                        "nameCN": "系统健壮性",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 24,
                        "caseNum": "3.1.110",
                        "nameEN": "Memory Exhaustion",
                        "nameCN": "内存耗尽",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 25,
                        "caseNum": "3.1.120",
                        "nameEN": "CPU Exhaustion",
                        "nameCN": "CPU耗尽",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 26,
                        "caseNum": "3.1.040",
                        "nameEN": "Internal Storage Misuse",
                        "nameCN": "内部存储滥用",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 27,
                        "caseNum": "3.1.100",
                        "nameEN": "Application Eviction",
                        "nameCN": "核心应用卸载",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 28,
                        "caseNum": "3.1.190",
                        "nameEN": "Flow Rule Flooding",
                        "nameCN": "流规则洪泛",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 29,
                        "caseNum": "3.1.200",
                        "nameEN": "Switch Firmware Misuse",
                        "nameCN": "交换机固件滥用",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },{
                        "id": 30,
                        "caseNum": "2.1.130",
                        "nameEN": "Memory Corruption",
                        "nameCN": "内存破坏",
                        "state": "READY",
                        "result": "UNKNOWN"
                    },
                ]
            }
        },
        methods: {
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            run(){
                for(var k in this.multipleSelection){
                    this.multipleSelection[k].state = 'RUNNING';
                    if(k != 0){
                        this.multiSelecNum += ',';
                    }else{
                        this.multiSelecNum += this.multipleSelection[k].caseNum;
                    }
                }
                // window.console.log("multiSelecNum: "+this.multiSelecNum.toString())
                axios({
                    method: 'post',
                    url: '/case/post',
                    data: this.multiSelecNum,
                }).then(res => {
                    if(res.data.code === 200){
                        window.console.log("post to run  and get ok")
                    }
                })
            },
            tableRowClassName({rowIndex}) {
                if (this.testFail.includes(rowIndex + 1)) {
                    this.tableData[rowIndex].result = 'FAIL';
                    return 'warning-row';
                } else if (this.testPass.includes(rowIndex + 1)) {
                    this.tableData[rowIndex].result = 'PASS';
                    return 'success-row';
                }
                return '';
            },
            getResults(){
                axios({
                    method: 'get',
                    url: '/case/get',
                }).then(res => {
                    //获取json中的data部分
                    // jsonObj=JSON.parse(res.data.data);
                    window.console.log("results:"+JSON.stringify(res.data))
                    var jsonObj = JSON.parse(JSON.stringify(res.data.data));
                    this.testFail = jsonObj.fail;
                    this.testPass = jsonObj.pass;
                })
            }
        },
        created() {
            this.timer = setInterval(this.getResults, 1000);
        },
        beforeDestroy() {
            clearInterval(this.timer);
        }
    }
</script>

<style>
    .el-table .warning-row {
        background: #EE3B3B;
        color: white;
    }

    .el-table .success-row {
        background: #76EE00;
    }
</style>